//
//  StatusCell.swift
//  SinaBlog
//
//  Created by macs on 15/11/14.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

let statuscellMargin : CGFloat = 12
let statusiconWidth = 35
import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    var cellConstraint : Constraint?
    var status : Status? {
        
        didSet{
            original.status = status
            
            //cell 重用时需要更新约束
            //取消约束
             self.cellConstraint?.uninstall()
            if let re = status?.retweeteds {
                //有转发微博存在
                retweeted.retweeteds = re
                retweeted.hidden = false
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.cellConstraint = make.top.equalTo(retweeted.snp_bottom).constraint
                })
                
            } else {
                //没有转发微博时
                retweeted.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.cellConstraint = make.top.equalTo(original.snp_bottom).constraint
                })
                
            }
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //2.添加子控件,设置约束
    func setupUI() {
        contentView.addSubview(original)
        
        contentView.addSubview(retweeted)
        
        contentView.addSubview(bottomView)
        
        
        original.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //make.height.equalTo(50)
        }
        
        retweeted.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(original.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //make.height.equalTo(0)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            self.cellConstraint = make.top.equalTo(retweeted.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
        
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.right.left.equalTo(self)
        }
        
    }
    //1.懒加载控件
    //懒加载
    lazy var original : OriginalView = OriginalView()
    lazy var bottomView : BottomView = BottomView()
    lazy var retweeted : RetweetedView  = RetweetedView()
    
}
